package com.ss.web.tagmark.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ss.web.tagmark.entity.TagExtend;
import com.ss.web.tagmark.entity.TagInfo;
import com.ss.web.tagmark.service.TagService;

/**
 * 标签管理相关的操作
 *
 * @author taofucheng
 *
 */
@Controller
@RequestMapping("/tags")
public class TagController extends BaseController {
	@Autowired
	private TagService tagService;

	/**
	 * 用户管理的首页
	 *
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(@ModelAttribute("tag") TagExtend tag, HttpServletRequest request, Model model) throws IOException {
		Page<TagInfo> tags = tagService.getListByPage(request.getParameterMap(), tag);
		model.addAttribute("tags", tags);
		model.addAttribute("users", tag.getUsers());
		return "tag/workspace";
	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@ResponseBody
	public List<TagInfo> get(String q) throws IOException {
		return tagService.find4Search(q);
	}

	@RequestMapping(value = "/{tagId:\\d+}/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public boolean delete(@PathVariable Long tagId, Model model, HttpServletResponse response) throws IOException {
		try {
			tagService.deleteById(tagId);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}
}
